<?php
namespace Common\Models;
use Common\BaseModel;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class NoticesUsed extends BaseModel
{
	public $res;
	public $expire;
    public function initialize()
    {
        parent::initialize();
        $this->setSource('f_notices_used');
    }


    /**
     * 获取读取过得公告
     * @param $user_id
     */
    public function is_used_ids($user_id){
        $condition = "user_id={$user_id}";
        $list=$this::find(array(
           $condition,
            'cloumns'=>'n_id'
        ))->toarray();
        if($list){
            $n_ids='';
            foreach($list as $key=>$value){
                $n_ids .=$value['n_id'].',';
            }
            $n_ids=substr($n_ids,0,strlen($n_ids)-1);
            return $n_ids;
        }else{
            return -2;
        }
    }



    /**
     * @param $user_id
     * 用户读取公告
     */
    public function is_read_used($user_id,$n_id){
        $NoticesDb = new Notices();
        $notices = $NoticesDb->findFirstById($n_id);
        $conditions['conditions'] = "n_id = '{$n_id}' and user_id = '{$user_id}'";
        $user_info = $this->findFirst($conditions);
        if(!$user_info){
            $this->user_id=$user_id;
            $this->created_at=date('Y-m-d H:i:s');
            $this->n_id=$n_id;
            $this->type = $notices->type;
            $this->save();
        }
    }


    /**
     * 用户读取公告
     */
    public function is_read($user_id,$n_id){
        $result = $this->findFirst("n_id ='{$n_id}' and user_id = '{$user_id}'");
        return $result ? true : false;
    }

}
